package com.heatup.apt.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.heatup.apt.common.exception.OrderException;
import com.heatup.apt.api.service.DataDictService;
import com.heatup.apt.api.service.MealService;
import com.heatup.apt.api.service.OrderService;
import com.heatup.apt.api.service.UserMealCouponService;
import com.heatup.apt.api.service.UserService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.dao.OrderDetailMapper;
import com.heatup.apt.common.dao.ServicePriceMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;
import com.heatup.apt.model.user.UserMealCoupon;
import com.heatup.apt.model.vo.CasherResolveLog;
import com.heatup.apt.model.vo.MealListVO;


@Controller
@RequestMapping(value = "/meal")
public class MealController extends BaseController {
	
	@Resource
	private OrderService orderService;

	@Resource
	private UserMealCouponService userMealCouponService;
	
	@Resource
	private DataDictService dataDictService;
	
	@Resource
	private OrderDetailMapper orderDetailMapper;
	
	@Resource
	private ServicePriceMapper servicePriceMapper;
	
	@Resource
	private UserService userService;
	
	@Resource
	private MealService mealService;
	
	/**
	 * 获取用户总餐券数
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/meal_count", method = {RequestMethod.GET})
	public String getMealCountInfo(HttpServletRequest request) {
		UserInfo userInfo = this.getUserInfo();
		Map<String,Object> parmsMap = new HashMap<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		UserMealCoupon umc = new UserMealCoupon();
		umc.setUserId(this.getUserId());
		umc = userMealCouponService.selectOne(umc);
		if(umc == null) {
			parmsMap.put("mealCount", 0);
		} else {
			parmsMap.put("mealCount", umc.getMealCount());
		}
		parmsMap.put("retailBeginTime", dataDictService.getMealRetailBeginTime());
		parmsMap.put("nowDate", DateUtils.getDateShort(new Date()));
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(parmsMap);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 得到餐券总数
	 * @return
	 */
	public UserMealCoupon getMealCount() {
		UserMealCoupon umc = new UserMealCoupon();
		umc.setUserId(this.getUserId());
		umc = userMealCouponService.selectOne(umc);
		return umc;
	}
	
	/**
	 * 餐券下单
	 * @throws OrderException 
	 */
	@ResponseBody
    @RequestMapping(value = "/meal_pay_order", method = {RequestMethod.POST, RequestMethod.GET})
	@Transactional
	public String payMealOrder(HttpServletRequest request) throws OrderException {
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		apiResultModel = mealService.dealOrder(request, apiResultModel, this.getUserInfo());
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 获取页面基本配置  暂时不用
	 */
	@ResponseBody
    @RequestMapping(value = "/get_meal_dict", method = {RequestMethod.POST, RequestMethod.GET})
	public String getBaseData(HttpServletRequest request) {
		UserInfo userInfo = this.getUserInfo();
		Map<String,Object> parmsMap = new HashMap<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		apiResultModel.setLogined(true);
		int count = orderService.getUserMealCount(this.getUserId());
		parmsMap.put("mealCount", count);
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(parmsMap);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	
	/**
	 * 餐券消费记录
	 */
	@ResponseBody
    @RequestMapping(value = "/get_meal_done_list", method = {RequestMethod.POST, RequestMethod.GET})
	public String getMealCount(HttpServletRequest request) {
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		
		Integer pageSize = RequestTools.RequestInt(request, "pageSize", 10);
		Integer pageIndex = RequestTools.RequestInt(request, "pageIndex", 0);
		pageIndex = pageIndex == 1 ? 0 * pageSize : pageIndex * pageSize;
		Map<String, Integer> param = ImmutableMap.of("pageSize", pageSize,"pageIndex", pageIndex,"userId", this.getUserId());
		MealListVO mealListVO = orderService.queryMealDetailCount(this.getUserId());
		List<MealListVO> list = orderService.queryMealDetail(param);
		ImmutableMap<String, Object> m = ImmutableMap.of("hadDoneMealCount", mealListVO.getSumMeal(), "list", list, "mealCount",mealListVO.getMealCount());
		apiResultModel.setLogined(true);
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(m);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 用餐订单详情
	 */
	@ResponseBody
    @RequestMapping(value = "/get_meal_order_detail", method = {RequestMethod.POST, RequestMethod.GET})
	public String getMealOrderDetail(HttpServletRequest request) {
		Integer orderId = RequestTools.RequestInt(request, "orderId", 0);
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		if(orderId == 0) {
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_ID_PARAM_WRONG.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.ORDER_ID_PARAM_WRONG.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		MealListVO mealVo = orderService.queryMealOrderDetail(orderId);
		apiResultModel.setLogined(true);
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(mealVo);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 获取明天用餐的订单
	 */
	@ResponseBody
    @RequestMapping(value = "/get_tomrrow_meal_detail", method = {RequestMethod.POST, RequestMethod.GET})
	public String getTomrrowMealDetail(HttpServletRequest request) {
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		if(!userService.checkUserHasMealRole(userInfo)) {
			apiResultModel.setMsg("您没有该权限！");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		String dateTime = RequestTools.RequestString(request, "dateTime", "");
		/**
	     * 私教消费餐
	     */
	    List<MealListVO> listpri = orderService.queryTomrrowMealList(dateTime);
	    /**
	     * 私教餐汇总
	     */
	    MealListVO priMealSum = orderService.queryTomrrowMealSum(dateTime);
	    /**
	     * 团课汇总
	     */
	    List<MealListVO> teamMeal =  orderService.queryTomrrowTeamMealList(dateTime);
	    /**
	     * 团课人数汇总
	     */
	    int teamSum = orderService.queryAllTeamPersonByDay(dateTime);
	    apiResultModel.setLogined(true);
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(ImmutableMap.of("listpri", listpri, "priMealSum", priMealSum, "teamMeal", teamMeal, "teamSum", teamSum));
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 获取明天用餐的订单
	 */
	@ResponseBody
    @RequestMapping(value = "/get_casher_log_detail", method = {RequestMethod.POST, RequestMethod.GET})
	public String getCasherLogDetail(HttpServletRequest request) {
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		if(!userService.checkUserHasMealRole(userInfo)) {
			apiResultModel.setMsg("没有权限！");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		String dateTime = RequestTools.RequestString(request, "dateTime", "");
		List<CasherResolveLog> list = orderService.queryCasherLog(userInfo.getId(), dateTime);
		CasherResolveLog crl = orderService.queryCasherSumMoney(userInfo.getId(), dateTime);
		apiResultModel.setLogined(true);
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(ImmutableMap.of("list", list, "sumMoney", crl.getSumAmount(), "personTotal", crl.getCountPerson()));
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
}
